import CreateAxiosInstance from "@/service/request";
import {ElLoading, ElMessage} from 'element-plus'
import router from '@/router/index'

const request = new CreateAxiosInstance({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  withCredentials: false,
  timeout: 5000,
  // headers: {'makabaka': 1127},
  load: false
})

// 添加请求拦截器逻辑，用于携带token
request.instance.interceptors.request.use(
  config => {
    // 从sessionStorage中获取userInfo
    const userInfo = sessionStorage.getItem('token');
    if (userInfo) {
      try {
        const { token } = JSON.parse(userInfo);
        // 如果token存在，则添加到请求头
        if (token) {
          config.headers['Authorization'] = `Bearer ${token}`;
        }
      } catch (e) {
        console.error('解析userInfo失败:', e);
      }
    }
    // 调用原有的请求开始回调
    if (request.requestStart) {
      request.requestStart(config);
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);
// 添加响应拦截器逻辑，用于处理响应数据和错误
request.instance.interceptors.response.use(
  response => {
    // 调用原有的请求结束回调
    if (request.requestEnd) {
      request.requestEnd(response.config);
    }
    return response.data;
  },
  error => {
    // 调用原有的请求结束回调
    if (error.config && request.requestEnd) {
      request.requestEnd(error.config);
    }

    // 处理401错误
    if (error.response && error.response.status === 401) {
      // 清除本地存储的token
      sessionStorage.removeItem('token');
      // 跳转到登录页
      window.location.hash = '#/login';
      ElMessage.error(ErrMessageInfo.timeoutMessage);
      return Promise.reject(error);
    }

    // 其他错误处理
    let errorMessage = ErrMessageInfo.errorMessage;
    if (error.response) {
      switch (error.response.status) {
        case 403:
          errorMessage = ErrMessageInfo.errMsg403;
          break;
        case 404:
          errorMessage = ErrMessageInfo.errMsg404;
          break;
        case 500:
          errorMessage = ErrMessageInfo.errMsg500;
          break;
        // 可以添加更多状态码的处理
        default:
          errorMessage = `${ErrMessageInfo.errorTip}: ${error.response.status}`;
      }
    } else if (error.request) {
      errorMessage = ErrMessageInfo.apiTimeoutMessage;
    } else {
      errorMessage = ErrMessageInfo.errorMessage;
    }

    // ElMessage.error(errorMessage);
    return Promise.reject(error);
  }
);


const ErrMessageInfo = {
  operationFailed: '操作失败',
  errorTip: '错误提示',
  errorMessage: '操作失败,系统异常!',
  timeoutMessage: '登录超时,请重新登录!',
  apiTimeoutMessage: '接口请求超时,请刷新页面重试!',
  apiRequestFailed: '请求出错，请稍候重试',
  networkException: '网络异常',
  networkExceptionMsg: '网络异常，请检查您的网络连接是否正常!',
  canceledError: '已终止请求',
  errMsg401: '用户没有权限（令牌、用户名、密码错误）!',
  errMsg403: '用户得到授权，但是访问是被禁止的。!',
  errMsg404: '网络请求错误,未找到该资源!',
  errMsg405: '网络请求错误,请求方法未允许!',
  errMsg408: '网络请求超时!',
  errMsg500: '服务器错误,请联系管理员!',
  errMsg501: '网络未实现!',
  errMsg502: '网络错误!',
  errMsg503: '服务不可用，服务器暂时过载或维护!',
  errMsg504: '网络超时!',
  errMsg505: 'http版本不支持该请求!'
}

let requestNum = 0
let fullscreenInstance

request.requestStart = (config) => {
  if (config && config.load) {
    if (requestNum <= 0) {
      fullscreenInstance = ElLoading.service({
        lock: true,
        text: 'Loading',
        background: 'rgba(0, 0, 0, 0.05)'
      })
    }
    requestNum += 1;
  }
}

request.requestEnd = (config) => {
  if (config && config.load) {
    requestNum -= 1
    if (requestNum <= 0) {
      fullscreenInstance?.close()
    }
  }
}

request.dataFactory = (res) => {
  if (res.status === 200 && res.data) {
    const {code, data, msg} = res.data
    if ( code > 0 || res.data ) {
      return res;
    } else {
      ElMessage.error(msg)
      return Promise.reject(res.data)
    }
  }
  Promise.reject({message: ErrMessageInfo.errorMessage})
}
request.requestError = (error) => {
  const {response, code, message} = error || {}
  if (code === 'ECONNABORTED' && message.indexOf('timeout') !== -1) {
    // ElMessage.error('请求超时')
    return Promise.reject({message: ErrMessageInfo.apiTimeoutMessage})
  }
  if (response) {
    const errMessage = checkStatus(response.status)
    if (errMessage) {
      ElMessage.error(errMessage)
      return Promise.reject({
        code: response.status,
        message: errMessage
      })
    }
  }
  return Promise.reject(error)
}

/**
 * 检查状态码并返回对应的错误信息
 * @param {number} code - HTTP状态码
 * @returns {string|null} - 对应的错误信息，如果没有匹配则返回null
 */
function checkStatus(code) {
  let errMessage  // 用于存储错误信息的变量
  // 使用switch语句根据不同的HTTP状态码设置对应的错误信息
  switch (code) {
    case 400:
      errMessage = ErrMessageInfo.errMsg401  // 400错误使用401的错误信息
      break
    // 401: Not logged in
    case 401:
      //
      errMessage = ErrMessageInfo.errMsg401
      break
    case 403:
      errMessage = ErrMessageInfo.errMsg403
      break
    case 404:
      errMessage = ErrMessageInfo.errMsg404
      break
    case 405:
      errMessage = ErrMessageInfo.errMsg405
      break
    case 408:
      errMessage = ErrMessageInfo.errMsg408
      break
    case 500:
      errMessage = ErrMessageInfo.errMsg500
      break
    case 501:
      errMessage = ErrMessageInfo.errMsg501
      break
    case 502:
      errMessage = ErrMessageInfo.errMsg502
      break
    case 503:
      errMessage = ErrMessageInfo.errMsg503
      break
    case 504:
      errMessage = ErrMessageInfo.errMsg504
      break
    case 505:
      errMessage = ErrMessageInfo.errMsg505
      break
    default:
  }
  return errMessage
}

export default request
